package controller

import (
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/net/ghttp"
	"seawolf-admin-go/internal/request/BaseReq"
	"seawolf-admin-go/internal/service/BaseService"
	"seawolf-admin-go/utility/response"
)

type IBaseController interface {
	Add(r *ghttp.Request) *response.JsonResponse
	Delete(r *ghttp.Request) *response.JsonResponse
	Update(r *ghttp.Request) *response.JsonResponse
	Info(r *ghttp.Request) *response.JsonResponse
	List(r *ghttp.Request) *response.JsonResponse
	Page(r *ghttp.Request) *response.JsonResponse
}

type BaseController struct {
	Req *BaseReq.I
}

func (c *BaseController) SetReq(req *BaseReq.I) {
	c.Req = req
}

func (c *BaseController) CheckId(r *ghttp.Request) error {
	tmp := r.Get("id", "")
	if tmp.IsNil() || tmp.IsEmpty() {
		return gerror.New("ID不能为空")
	}
	c.Req.Id = tmp.String()
	return nil
}
func (c *BaseController) checkIds(r *ghttp.Request) error {
	tmp := r.Get("ids", "")
	if tmp.IsNil() || tmp.IsEmpty() {
		return gerror.New("IDS不能为空")
	}
	c.Req.Ids = tmp.Strings()

	return nil
}

func (c *BaseController) Init(r *ghttp.Request) {

}

func (c *BaseController) Shut(r *ghttp.Request) {

}

// 如果 api 为空，则注册所有的方法
func RegBase[T IBaseController](group *ghttp.RouterGroup, c T, apis []string) {
	if apis == nil || len(apis) == 0 {
		apis = []string{"List", "Page", "Info", "Add", "Update", "Delete"}
	}
	for _, api := range apis {
		switch api {
		case "List":
			group.POST("/list", func(r *ghttp.Request) {
				res := c.List(r)
				if res == nil {
					res = response.FailByRequestMessage(r, "方法未实现")
				}
				r.Response.WriteJson(res)
			})
		case "Page":
			group.POST("/page", func(r *ghttp.Request) {
				res := c.Page(r)
				if res == nil {
					res = response.FailByRequestMessage(r, "方法未实现")
				}
				r.Response.WriteJson(res)
			})
		case "Info":
			group.GET("/info", func(r *ghttp.Request) {
				res := c.Info(r)
				if res == nil {
					res = response.FailByRequestMessage(r, "方法未实现")
				}
				r.Response.WriteJson(res)
			})
		case "Add":
			group.POST("/add", func(r *ghttp.Request) {
				res := c.Add(r)
				if res == nil {
					res = response.FailByRequestMessage(r, "方法未实现")
				}
				r.Response.WriteJson(res)
			})
		case "Update":
			group.POST("/update", func(r *ghttp.Request) {
				res := c.Update(r)
				if res == nil {
					res = response.FailByRequestMessage(r, "方法未实现")
				}
				r.Response.WriteJson(res)
			})
		case "Delete":
			group.POST("/delete", func(r *ghttp.Request) {
				res := c.Delete(r)
				if res == nil {
					res = response.FailByRequestMessage(r, "方法未实现")
				}
				r.Response.WriteJson(res)
			})
		}
	}
}

func (c *BaseController) Add(r *ghttp.Request) *response.JsonResponse {
	return BaseService.Add(r, c.Req)
}

func (c *BaseController) Delete(r *ghttp.Request) *response.JsonResponse {
	c.Req.Ids = r.Get("ids", "").Strings()
	return BaseService.Delete(r, c.Req)
}

func (c *BaseController) Update(r *ghttp.Request) *response.JsonResponse {
	c.Req.Id = r.Get("id", "").String()
	return BaseService.Update(r, c.Req)
}

func (c *BaseController) Info(r *ghttp.Request) *response.JsonResponse {
	c.Req.Id = r.Get("id", "").String()
	return BaseService.Info(r, c.Req)
}

func (c *BaseController) List(r *ghttp.Request) *response.JsonResponse {
	return BaseService.List(r, c.Req)
}

func (c *BaseController) Page(r *ghttp.Request) *response.JsonResponse {
	if err := r.Parse(&c.Req.PageInfo); err != nil {
		return response.FailByRequestMessage(r, err.Error())
	}
	return BaseService.Page(r, c.Req)
}
